package com.cmes.system.core.service;

import com.cmes.core.utils.DateUtils;
import com.cmes.jdbc.service.GenericService;
import com.cmes.system.core.entity.LoginLogEntity;
import com.cmes.system.core.repository.LoginLogRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 登录日志
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class LoginLogService extends GenericService<LoginLogRepository, LoginLogEntity> {

    public void success(Long userId, String username, String loginType) {
        insert(LoginLogEntity.builder()
                .userId(userId)
                .loginId(username)
                .loginType(loginType)
                .loginTime(DateUtils.now())
                .loginIp("")
                .loginLocation("")
                .os("")
                .result(true)
                .build());
    }

    public void fail(String username, String loginType, String message) {
        insert(LoginLogEntity.builder()
                .userId(0L)
                .loginId(username)
                .loginType(loginType)
                .loginTime(DateUtils.now())
                .loginIp("")
                .loginLocation("")
                .os("")
                .result(false)
                .msg(message)
                .build());
    }

}
